from django.db import models

# Create your models here.


class Port(models.Model):
    port_id = models.AutoField(primary_key=True)
    name = models.CharField('端口名称', max_length=200)
    create_time = models.DateTimeField(auto_now_add=True)
    modify_time = models.DateTimeField(auto_now=True)
    pre_process = models.ForeignKey('PreProcess', verbose_name='预处理', on_delete=models.CASCADE)
    model = models.ForeignKey('Models', verbose_name='模型', on_delete=models.CASCADE)
    post_process = models.ForeignKey('PostProcess', verbose_name='后处理', on_delete=models.CASCADE)
    content = models.TextField('说明', max_length=500, blank=True)

    class Meta:
        verbose_name = '模型端口'
        verbose_name_plural = '模型端口'

    def __str__(self):
        return self.name


class PreProcess(models.Model):
    name = models.CharField('预处理名称', max_length=200)
    path = models.FileField('脚本文件(*.py)', max_length=200)
    para = models.TextField('维度数据', max_length=500, editable=False)
    create_time = models.DateTimeField(auto_now_add =True)
    modify_time = models.DateTimeField(auto_now=True)
    file = models.CharField(blank=True, editable=False, max_length=200)
    content = models.TextField('说明', max_length=500, blank=True)

    def save(self, *args, **kwargs):
        self.file = self.path.path.split('/')[-1].split('.')[0]
        super(PreProcess, self).save(*args, **kwargs)

    class Meta:
        verbose_name = '预处理'
        verbose_name_plural = '预处理'

    def __str__(self):
        return self.name


class Models(models.Model):
    name = models.CharField(verbose_name='模型名称', max_length=200, unique=True)
    path = models.FileField(verbose_name='脚本文件(*.py)', max_length=200)
    m_path = models.FileField(verbose_name='模型文件(*.m)', max_length=200)
    create_time = models.DateTimeField(auto_now_add=True)
    modify_time = models.DateTimeField(auto_now=True)
    content = models.TextField(verbose_name='说明', max_length=500, blank=True)
    file = models.CharField(blank=True, editable=False, max_length=200)
    m_file = models.CharField(blank=True, editable=False, max_length=200)

    class Meta:
        verbose_name = '模型过程'
        verbose_name_plural = '模型过程'

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        self.file = self.path.path.split('/')[-1].split('.')[0]
        self.m_file = self.m_path.path.split('/')[-1].split('.')[0]
        super(Models, self).save(*args, **kwargs)
        with open(self.path.path, 'r', encoding='utf-8') as f:
            lines = f.readlines()
            lines[5] = f"file_name = '{self.m_file}.m  # line_index=6'\n"
        with open(self.path.path, 'w+', encoding='utf-8') as f:
            f.write(''.join(lines))


class PostProcess(models.Model):
    name = models.CharField('后处理名称', max_length=200)
    path = models.FileField('脚本文件(*.py)', max_length=200)
    create_time = models.DateTimeField(auto_now_add=True)
    modify_time = models.DateTimeField(auto_now=True)
    content = models.TextField('说明', max_length=500, blank=True)
    file = models.CharField(blank=True, editable=False, max_length=200)

    class Meta:
        verbose_name = '后处理'
        verbose_name_plural = '后处理'

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        self.file = self.path.path.split('/')[-1].split('.')[0]
        super(PostProcess, self).save(*args, **kwargs)

